/**
 * @className: Brick.java
 * @package: default
 * @author: Sean O'Brien, Jack, Lester Tam, Erik Telstad
 * @date_created: November 16, 2009
 * @last_motified: November 16, 2009
 * 
 * 
 * 
 */
import java.io.File;
import java.io.Serializable;
//import java.util.Arrays;
import java.util.Observable;
//import java.util.Stack;

public class Brick extends Observable implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private File file;
	private double width;
    private double height;
    private double length;
    private double state;
    
	private boolean changed = false;


    
    /**
     * default constructor. All fields are initialized to 50
     */
    Brick(){
        height = 50;
        length = 50;
        width = 50;
        state = 0;
        
        setChanged();
        clearChanged();
    }
    
    /**
	 * @return the height
	 */
	public double getHeight() {
		return height;
	}

	/**
	 * @param height the height to set
	 */
	public void setHeight(double newHeight) throws IllegalArgumentException, NumberFormatException {
		if(newHeight <= 0)
			throw new IllegalArgumentException("Input must be greater than 0");
		state = height;
		this.height = newHeight;
		setChanged();
		clearChanged();
	}

	/**
	 * @return the length
	 */
	public double getLength() {
		return length;
	}

	/**
	 * @param length the length to set
	 */
	public void setLength(double newLength) throws IllegalArgumentException, NumberFormatException {
		if(newLength <= 0)
			throw new IllegalArgumentException("Input must be greater than 0");
		state = length;
		this.length = newLength;
		setChanged();
		clearChanged();
	}

	/**
	 * @return the width
	 */
	public double getWidth() {
		return width;
	}

	/**
	 * @param width the width to set
	 */
    public void setWidth(double newWidth) throws IllegalArgumentException, NumberFormatException {
		if(newWidth <= 0)
			throw new IllegalArgumentException("Input must be greater than 0");
		state = width;
    	this.width = newWidth;
		setChanged();
		clearChanged();
    }
    
	public File getFile() {
		return file;
	}

	public void setFile(File newFile) {
		file = newFile;
	}

	public boolean isChanged() {
		return changed;
	}

	public void setChanged(boolean changed) {
		this.changed = changed;
	}

	/**
	 * notifies observers of changes and sets changed flag to true
	 */
	@Override
	public void setChanged() {
		super.setChanged();
		this.notifyObservers();
		this.changed = true;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Brick [height=" + height + ", length=" + length + ", width=" + width
				+ ", state=" + state + "]";
	}
	
	public BrickMemento makeMemento() {
		BrickMemento mem = new BrickMemento();
		//System.out.println("the length " + length);
		//System.out.println("the heigth " + height);
		//System.out.println("the width " + width);
		return mem;
	}
	
	public void restore(Memento aMemento) {
		BrickMemento temp = (BrickMemento) aMemento;
		setLength(temp.theLength);
		//System.out.println("length " + length);
		setHeight(temp.theHeight);
		//System.out.println("height " + height);
		setWidth(temp.theWidth);
		//System.out.println("width " + width);
		//System.out.println("\n");
		setChanged();
		clearChanged();
	}
	
	private class BrickMemento implements Memento {
		private double theHeight;  // the state from Brick.java
		private double theLength;
		private double theWidth;
		
		public BrickMemento() {
			//super();
			this.theHeight = height;//theHeight;
			this.theLength = length;//theLength;
			this.theWidth = width;//theWidth;
		}

		/**
		 * @return the theHeight
		 */
		public double getTheHeight() {
			return theHeight;
		}
		
		/**
		 * @return the theLength
		 */
		public double getTheLength() {
			return theLength;
		}
		
		/**
		 * @return the theWidth
		 */
		public double getTheWidth() {
			return theWidth;
		}

	}
			

}
